<?php
namespace Deployer;

require 'recipe/yii2-app-basic.php';

// Project name
set('application', 'my_project');

// Project repository
set('repository', 'https://gitee.com/xu_chen813/ai.git');

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true); 

// Shared files/dirs between deploys 
add('shared_files', []);
add('shared_dirs', []);

// Writable dirs by web server 
add('writable_dirs', []);


// Hosts
set('deploy_path','/var/www/html/deployer');
host('212.129.141.254')
    ->stage('prod')
    ->user('deployer')
    ->set('deploy_path', get('deploy_path'))
    ->set('cachetool', '/var/run/php/php7.2-fpm.sock');  
    


// Tasks


// // 删除一个月前的日志文件
// task('delete_logs', function () use ($apps){
//     $apps[] = 'console';
//     foreach ($apps as $app) {
//         $application_15 = 'cd '.get('deploy_path').'/shared/'.$app.'/runtime/logs && find '.get('deploy_path').'/shared/'.$app.'/runtime/logs/ -mtime +15 -type f -name "application-*" | xargs rm -rf';
//         run($application_15);
//         // echo $application_15.PHP_EOL;
//         $info_15 = 'cd '.get('deploy_path').'/shared/'.$app.'/runtime/logs && find '.get('deploy_path').'/shared/'.$app.'/runtime/logs/ -mtime +15 -type f -name "info-*" | xargs rm -rf';
//         run($info_15);
//         // echo $info_15.PHP_EOL;
//     }
// })->desc('delete logs');

    // composer 参数，测试环境跟正式环境不一样
    task('composer_dev', function () {
        $stage = $_SERVER['argv'][0];
        $no_dev = '';
        if ($stage == 'prod') {
            // 正式环境不需要dev
            $no_dev = '--no-dev';
        }
        set('composer_options', '{{composer_action}} --verbose --prefer-dist --no-progress --no-interaction '.$no_dev.' --optimize-autoloader');
    })->desc('composer install');
    
task('build', function () {
    run('cd {{release_path}} && build');
});

// 自定义任务：重启 php-fpm 服务
task('php-fpm:restart', function () {
    run('systemctl restart php7.2-fpm.service');
});
task('deploy', [
    'deploy:info',
    'deploy:prepare',
    'deploy:lock',
    'deploy:release',
    'deploy:update_code',
    // 'delete_logs', //删除各种日志文件
    'deploy:shared',
    'deploy:writable',
    // 'index_cp',
 //   'cp_wx_verify_txt',
    'composer_dev',
    'deploy:vendors',
    'deploy:clear_paths',
    // 'deploy:run_migrations',
    //'deploy:supervisor_config', // 需要运行的时候放开
    // 'crontab_set', // 需要设置的时候放开
 //   'nginx_config', // 需要设置的时候放开
    'deploy:symlink',
    'deploy:unlock',
    'cleanup',
    'success'
])->desc('Deploy your project');

// after('deploy:symlink', 'cachetool:clear:opcache');

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

